import Vue from "vue";
import {
  getDictObj,
  parseTime,
  resetForm,
  addDateRange,
  selectDictLabel,
  selectDictLabels,
  handleTree
} from "@/utils/ruoyi";
import moment from 'moment'//导入文件
import {download} from '@/utils/request'
import {getDicts} from "@/api/system/dict/data";
import {getConfigKey} from "@/api/system/config";
import storage from '@/utils/localStorage'

import {deepClone, deepClone2} from "@/utils";


const install = (Vue) => {
  //深拷贝
  Vue.prototype.deepClone = deepClone2

  //存储
  Vue.prototype.setLocalStorage = storage.set
  Vue.prototype.getLocalStorage = storage.get
  Vue.prototype.removeLocalStorage = storage.remove

  //时间插件
  Vue.prototype.$moment = moment//赋值使用

// 全局方法挂载
  Vue.prototype.getDicts = getDicts
  Vue.prototype.getConfigKey = getConfigKey
  Vue.prototype.getDictObj = getDictObj
  Vue.prototype.download = download
  Vue.prototype.handleTree = handleTree
  Vue.prototype.bmNormalizer = function (node) {
    if (node.children && !node.children.length) {
      delete node.children;
    }
    return {
      id: node.bmId,
      label: node.name,
      children: node.children
    };
  }
  Vue.prototype.parseTime = parseTime
  Vue.prototype.resetForm = resetForm
  Vue.prototype.addDateRange = addDateRange
  Vue.prototype.selectDictLabel = selectDictLabel
  Vue.prototype.selectDictLabels = selectDictLabels

  Vue.prototype.msgSuccess = function (msg) {
    this.$message({showClose: true, message: msg, type: "success"});
  }

  Vue.prototype.msgError = function (msg) {
    this.$message({showClose: true, message: msg, type: "error"});
  }

  Vue.prototype.msgInfo = function (msg) {
    this.$message.info(msg);
  }


  Vue.prototype.isEmpty = function (val) {
    // null or undefined
    if (val == null) return true;

    if (typeof val === 'boolean') return false;

    if (typeof val === 'number') return !val;

    if (val instanceof Error) return val.message === '';

    switch (Object.prototype.toString.call(val)) {
      // String or Array
      case '[object String]':
      case '[object Array]':
        return !val.length;

      // Map or Set or File
      case '[object File]':
      case '[object Map]':
      case '[object Set]': {
        return !val.size;
      }
      // Plain Object
      case '[object Object]': {
        return !Object.keys(val).length;
      }
    }

    return false;
  };

  Vue.prototype.returntomenu = function () {
    setTimeout(() => {
      this.$store.dispatch('tagsView/delView', this.$route)
      this.$router.go(-1)
    }, 1000)
  }

// data转换
  Vue.prototype.formData = function (obj) {
    const formData = new FormData()
    Object.keys(obj).forEach((key) => {
      formData.append(key, obj[key])
    })
    return formData
  }


  Vue.prototype.numToCny = (n) => {
    let fraction = ['角', '分']
    let digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
    let unit = [['元', '万', '亿'], ['', '拾', '佰', '仟']];
    let head = n < 0 ? '欠' : ''
    n = Math.abs(n)
    let s = ''
    for (let i = 0; i < fraction.length; i++) {
      s += (digit[Math.floor(Math.floor(n * 1000 * 10 * Math.pow(10, i)) % (10 * 1000) / 1000)] + fraction[i]).replace(/零./, '')
    }
    s = s || '整'
    n = Math.floor(n)
    for (let i = 0; i < unit[0].length && n > 0; i++) {
      let p = ''
      for (let j = 0; j < unit[1].length && n > 0; j++) {
        p = digit[n % 10] + unit[1][j] + p
        n = Math.floor(n / 10)
      }
      s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s
    }
    return head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整')
  }
}

export default {
  install
}
